package com.shengyo.model.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.shengyo.model.bean.Digital;
import com.shengyo.model.bean.Mobile;
import com.shengyo.model.dao.IMobileDAO;


public class MobileDAOImpl extends BaseDAOImpl implements IMobileDAO{

	public void delete(Mobile mobile) {
		this.getHibernateTemplate().delete(mobile);
		
	}
	
	@SuppressWarnings("unchecked")
	public Mobile findByWaiguan(String waiguan) {
		List list = this.getHibernateTemplate().find("from mobile m where m.waiguan=?",new Object[]{waiguan});
		if (list != null && !list.isEmpty())
			return (Mobile) list.get(0);
		else
			return null;
	}

	@SuppressWarnings("unchecked")
	public List<Digital> findAll() {
		
		return this.getHibernateTemplate().find("from  mobile");
	}

	
	@SuppressWarnings("unchecked")
	public List<Digital> findAll(final int first, final int max) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				return session.createQuery("from mobile").setFirstResult(first).setMaxResults(max).list();
			}
		});
	}

	@Override
	public Mobile findById(String id) {
		return (Mobile) this.getHibernateTemplate().load(Mobile.class, id);
	}

	@Override
	public Mobile findByMobileUrl(String url) {
		List<?> list = this.getHibernateTemplate().find("from mobile m where m.url=?",new Object[]{url});
		if (list != null && !list.isEmpty())
			return (Mobile) list.get(0);
		else
			return null;
	}

	@Override
	public Object findById(int id) {
		return null;
	}

}
